package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.DishDto;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.entity.DishFlavor;
import com.itheima.reggie.service.DishFlavorService;
import com.itheima.reggie.service.DishService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/dish")
@Slf4j
public class DishController {
    @Autowired
    private DishService dishService;
    @Autowired
    private DishFlavorService dishFlavorService;


    @GetMapping("/{id}")
    public R getById(@PathVariable Long id){

        return dishService.getByIdDish(id);
    }
    @PostMapping()
    public R save(@RequestBody DishDto dish){
        log.info("菜品名称{}",dish.toString());
        return dishService.saveWithFlavor(dish);
    }
    @GetMapping("/page")
    public R<Page> page(int page , int pageSize, String name){
        Page<Dish> pageInfo = new Page<>(page,pageSize);
        LambdaQueryWrapper<Dish> lqw = new LambdaQueryWrapper();
        lqw.like(StringUtils.isNotEmpty(name), Dish::getName,name).orderByAsc(Dish::getSort).orderByDesc(Dish::getUpdateTime);

        dishService.page(pageInfo,lqw);


        return R.success(pageInfo);
    }

    @PutMapping
    public R update(@RequestBody  DishDto dishDto){

        return dishService.updateDish(dishDto);
    }

    @PostMapping("/status/{id}")
    public R status(@PathVariable int id,Long[] ids){

        return dishService.updateDishStatus(id,ids);
        //return null;
    }
    @DeleteMapping
    public R delete(@RequestParam List<Long> ids){
        return dishService.deleteDish(ids);
    }
    @GetMapping("/list")
    public R list(DishDto dishDto){
        LambdaQueryWrapper<Dish> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Dish::getCategoryId,dishDto.getCategoryId()).eq(Dish::getStatus,1).orderByAsc(Dish::getSort).orderByDesc(Dish::getUpdateTime);
        List<Dish> dishs = dishService.list(lqw);

        List<Long> dishList = dishs.stream().map(dish -> dish.getId()).collect(Collectors.toList());


        LambdaQueryWrapper<DishFlavor> lqw2 = new LambdaQueryWrapper();
        lqw2.in(DishFlavor::getDishId,dishList);

        List<DishFlavor> dishFlavors = dishFlavorService.list(lqw2);


        List<DishDto> dishDtos = new ArrayList<>();

        dishs.forEach(dish -> {
            DishDto dishDto1 = new DishDto();
            BeanUtils.copyProperties(dish,dishDto1);
            List<DishFlavor> dishFlavorList = dishFlavors.stream().filter(dishFlavor -> dishFlavor.getDishId().equals(dish.getId())).collect(Collectors.toList());
            dishDto1.setFlavors(dishFlavorList);
            dishDtos.add(dishDto1);
        });

        return R.success(dishDtos);
    }

}
